package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.constans.ResultCode;
import com.qf.exceptions.AddDataException;
import com.qf.mapper.BookMapper;
import com.qf.mapper.CategoryMapper;
import com.qf.pojo.Book;
import com.qf.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BookServiceImpl implements BookService {

    private BookMapper bookMapper;
    private CategoryMapper categoryMapper;

    @Autowired
    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    @Autowired
    public void setCategoryMapper(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }

    @Override
    public void addBook(Book book) {
        if(book==null||book.getBookname()==null){
            throw new AddDataException("书籍重要信息不全", ResultCode.REPEAT_LOGIN);
        }
        bookMapper.addBook(book);
    }

    @Override
    public PageInfo<Book> findAllBookByPage(String name, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Book> bookList= bookMapper.findAllBook();

        PageInfo<Book> pageInfo = new PageInfo<>(bookList);
        return pageInfo;
    }
}
